package com.tsx.leetcode.hot100.easy;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * <a href="https://leetcode.cn/problems/invert-binary-tree">反转二叉树</a>
 * 1. 递归实现：先反转左右两颗树，再递归去反转他们的左子树和右子树
 * 2. 广度优先方法
 */
public class Q226InvertBinaryTree {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);
        // 打印原始树
        System.out.println("原始树:");
        printTree(root);
        TreeNode invertedRoot = invertTree(root);
        // 打印反转后的树
        System.out.println("\n反转后的树:");
        printTree(invertedRoot);
    }

    /**
     * 使用递归的方法反转二叉树
     *
     * @param root root节点
     * @return 返回翻转后的root节点
     */
    /*public static TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        // 此处是翻转左右子树
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        // 反转左子树的子树
        invertTree(root.left);
        // 反转右子树的子树
        invertTree(root.right);
        return root;
    }*/

    /**
     * 利用栈的特性做
     *
     * @param root root节点
     * @return 反转后的
     */
    public static TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        Stack<TreeNode> stack = new Stack<>() {{
            add(root);
        }};
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            // 只有当至少有一个子节点不为空时才需要交换
            if (pop.left != null || pop.right != null) {
                TreeNode tmp = pop.left;
                pop.left = pop.right;
                pop.right = tmp;

                // 只有子节点不为空时才需要入栈
                if (pop.left != null) {
                    stack.add(pop.left);
                }
                if (pop.right != null) {
                    stack.add(pop.right);
                }
            }
        }
        return root;
    }

    // 辅助方法：按层次打印树
    private static void printTree(TreeNode root) {
        if (root == null) return;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                System.out.print(node.val + " ");

                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            System.out.println();
        }
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}